/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package 解析.node.node;

import 解析.node.others.TreeInfo;
import 解析.Matcher;
import 解析.Pattern;

/**
 *
 * @author yanxi
 */
public class Ques extends Node { 
    public Node atom;
    public int type;

    public Ques(Node node, int type) {
        this.atom = node;
        this.type = type;
        getChildren().add(atom); 
    }
    
    @Override
    public boolean match(Matcher matcher, int i, CharSequence seq) {
        switch (type) {
            case Pattern.GREEDY:
                return (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq))
                        || next.match(matcher, i, seq);
            case Pattern.LAZY:
                return next.match(matcher, i, seq)
                        || (atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq));
            case Pattern.POSSESSIVE:
                if (atom.match(matcher, i, seq)) {
                    i = matcher.last;
                }
                return next.match(matcher, i, seq);
            default:
                return atom.match(matcher, i, seq) && next.match(matcher, matcher.last, seq);
        }
    }

    @Override
    public boolean study(TreeInfo info) {
        if (type != Pattern.INDEPENDENT) {
            int minL = info.minLength;
            atom.study(info);
            info.minLength = minL;
            info.deterministic = false;
            return next.study(info);
        } else {
            atom.study(info);
            return next.study(info);
        }
    } 
    
    @Override
    public String toString(){
        if(atom instanceof GroupHead)return atom.toString();
        return atom.toString()+"?";
    }
}
